import comm from './core';

function transform (data) {
  const {
    code,
    displayName,
    pic,
    price,
    description
  } = data.pack;
  const arrangeTreeElements = data.items.content || [];
  return {
    code,
    title: displayName,
    intro: description,
    picUrl: comm.formatZoomPic(pic),
    price: (price / 100).toFixed(2),
    list: arrangeTreeElements.map(item => { return transformList(item); }) || []
  };
}

function transformList (item) {
  const {
    displayName,
    pic,
    subtitle,
    contentCode,
    duration = 0,
    playCount
  } = item;
  return {
    code: contentCode,
    title: displayName,
    picUrl: comm.formatZoomPic(pic),
    subtitle,
    duration: comm.formatSecond(duration),
    playCount
  };
}

export function get () {
  const params = comm.requestParams();

  const {
    origin,
    code,
    status
  } = params;
  if (!status || status !== 1) {
    return {};
  }

  const path = `pack/findByCode`;
  const query = `code=${code}`;
  return comm.request({ origin, path, query }).then(({ data }) => {
    if (data) {
      return transform(data);
    }
  });
  // const service = comm.requestParams();
  // let origin, path, query;
  // if (service.status === 1) {
  //   const { url, code } = service;
  //   origin = url;
  //   path = `pack/findByCode`;
  //   query = `code=${code}`;
  //   return comm.json({ origin, path, query }).then(resp => {
  //     if (resp.data) {
  //       return transform(resp.data);
  //     }
  //   });
  // }
}

export default {
  get
};
